var express = require("express");
var router = express.Router();
const path = require("path");

const low = require("lowdb");
const FileSync = require("lowdb/adapters/FileSync");
const adapter = new FileSync(path.resolve(__dirname, "../utils/lowdb/db.json"));
const db = low(adapter);
// db.defaults({ accounts: [] }).write(); // 创建一个
const shortid = require("shortid");

/* GET home page. */
router.get("/", function (req, res, next) {
  res.render("index", { title: "Express" });
});

router.get("/account", function (req, res, next) {
  res.render("accountList");
});

router.get("/account/create", function (req, res, next) {
  res.render("accountCreate");
});

router.post("/account", (req, res, next) => {
  console.log("req", req.body);
  db.get("accounts")
    .push({ id: shortid.generate(), ...req.body })
    .write();

  res.render("success", { msg: "新增成功", url: "/account" });
});

module.exports = router;
